/**
 * This file is part of AppStoreBot.
 *
 *  AppStoreBot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AppStoreBot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AppStoreBot.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package lu.pragmaconsult.appstorebot.itunes;

import static lu.pragmaconsult.appstorebot.itunes.Constants.AJAX_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_LIST_AVAILABLE_DAYS;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_LIST_AVAILABLE_WEEKS;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.SALES_URL;
import static lu.pragmaconsult.appstorebot.itunes.Constants.VIEW_STATE;
import lu.pragmaconsult.appstorebot.BotContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

/**
 * TODO: Type comment
 * 
 * @author Wilfried Vandenberghe (wilfried.vandenberghe at pragmaconsult.lu)
 */
class SelectWeeklyReportsBotState extends AbstractBotState {

	private static final Logger logger = LoggerFactory.getLogger(SelectWeeklyReportsBotState.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * lu.pragmaconsult.appstorebot.itunes.AbstractBotState#createHttpMethod
	 * (lu.pragmaconsult.appstorebot.BotContext,
	 * org.apache.commons.httpclient.HttpClient)
	 */
	@Override
	protected HttpMethod createHttpMethod(BotContext context,
			HttpClient httpClient) {
		if(context.isVerbose()) {
			logger.info("Select weekly report");
		}
		
		PostMethod method = new PostMethod(SALES_URL);

		NameValuePair ajaxRequest = new NameValuePair("AJAXREQUEST", context.getContextAttribute(AJAX_NAME));
		NameValuePair theForm = new NameValuePair("theForm", "theForm");
		NameValuePair theFormNotNormal = new NameValuePair("theForm:xyz", "notnormal");
		NameValuePair theFormVendorType = new NameValuePair("theForm:vendorType", "Y");
		NameValuePair dateDay = new NameValuePair("theForm:datePickerSourceSelectElementSales", context.getContextAttribute(DATE_LIST_AVAILABLE_DAYS).split(",")[0]);
		NameValuePair dateWeek = new NameValuePair("theForm:weekPickerSourceSelectElement", context.getContextAttribute(DATE_LIST_AVAILABLE_WEEKS).split(",")[0]);
		NameValuePair viewState = new NameValuePair("javax.faces.ViewState", context.getContextAttribute(VIEW_STATE));
		NameValuePair select = new NameValuePair(context.getContextAttribute(DATE_NAME), context.getContextAttribute(DATE_NAME));
		method.setRequestBody(new NameValuePair[] { ajaxRequest, theForm, theFormNotNormal, theFormVendorType, dateDay, dateWeek, viewState, select });

		return method;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lu.pragmaconsult.appstorebot.itunes.AbstractBotState#getNextState()
	 */
	@Override
	protected BotState getNextState() {
		return new SelectDateBotState();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * lu.pragmaconsult.appstorebot.itunes.AbstractBotState#processResponseBody
	 * (lu.pragmaconsult.appstorebot.BotContext,
	 * org.apache.commons.httpclient.HttpMethod)
	 */
	@Override
	protected void processResponseBody(BotContext context, HttpMethod method) {
		try {
			HtmlParser parser = new HtmlParser(method.getResponseBodyAsStream());
			
			// TODO: replace following by XPath expression
			// get the view state value
			
			Node node = parser.getDocument().getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(2).getChildNodes().item(1);
			String state = node.getAttributes().getNamedItem("value").getNodeValue();
			context.setContextAttribute(VIEW_STATE, state);
			
		} catch(Exception e) {
			throw new IllegalStateException(e);
		}

	}

}
